package com.sign.signsystem;

import com.biz.service.PassWordService;
import com.biz.service.UserService;
import com.core.entity.AccessRight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class LoginActivity extends Activity {
	EditText nameET, passwordET;
	RadioGroup radio;
	Button mLoginBtn;
	LocationApplication application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		initComponent();
		application = (LocationApplication) getApplication();
		mLoginBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View paramView) {
				AccessRight accessRight = new PassWordService().getAccessRight(
						nameET.getText().toString(), passwordET.getText()
								.toString(), radio.getCheckedRadioButtonId());
				if (accessRight.equals(AccessRight.noRight)) {
					Toast.makeText(getApplicationContext(),
							R.string.warning_message, Toast.LENGTH_SHORT)
							.show();
					return;
				} else {
					application.setUserEntity(new UserService()
							.getUserDemo(nameET.getText().toString()));
					application.setAccessRight(accessRight);
				}
				Intent intent = new Intent(LoginActivity.this,
						MainActivity.class);
				startActivity(intent);
			}
		});
	}

	private void initComponent() {
		mLoginBtn = (Button) findViewById(R.id.login_btn);
		nameET = (EditText) findViewById(R.id.name_edittext);
		passwordET = (EditText) findViewById(R.id.password_edittext);
		radio = (RadioGroup) findViewById(R.id.radioGroup1);
		radio.getCheckedRadioButtonId();
	}

}
